<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="heard :: head"></head>

import mt_user
<nav th:replace="heard :: nav"></nav>

<body>
<label>lsession:</label><input id="lsession" value="">
<label>lenovoId:</label><input id="lenovoId" value="978880162@qq.com">
<label>cerpreg_passprt:</label><input id="cerpreg_passprt" >
<button onclick="ass()">submit</button>
</body>
<!--
<script src="/js/sso.js"></script>
-->
<script>
    var mt_user = {};
    var passportStAccess = "//passport.lenovo.com/wauthen5/stAccessLogin";
    var passportLogout = "//passport.lenovo.com/wauthen5/gateway?lenovoid.action=uilogout&lenovoid.realm=";
    var passportJS = "//passport.lenovo.com/wauthen5/wauth/js/LenovoId.js";
    var passportSyncCookie = "//passport.lenovo.com/wauthen5/synCookie.jhtml?lenovoid_realm=";
    var defaultRealm = "account.lenovo.com";

    mt_user.option = {
        realm: defaultRealm
    };


    //1. status,st,verify,id,email
    mt_user.init = function (option) {
        mt_user.option = option;
        if (!checkStr(mt_user.option.realm)) {
            mt_user.option.realm = defaultRealm;
        }
        mt_user.user = {
            status: option.isLogin
        }
        if (mt_user.user.status) {
            if (mt_user.option.cb) {
                mt_user.option.cb("");
            }
        } else {
            loadLenovoId();
        }
    }

    //2. load LenovoID confirm login status
    function loadLenovoId() {
        jQuery.getScript(passportJS, function (res, status) {
            console.log("LenovoId load " + status);
            if (status == "success") {
                jQuery.getScript(passportSyncCookie + mt_user.option.realm)
                    .done(function (script, textStatus) {
                        console.log("load lenovoid js synCookie load " + textStatus);
                    })
                    .fail(function (jqxhr, settings, exception) {
                        console.log(exception);
                    });
            }
        });
    }

    //3. lenovoID callback
    window.LenovoIdSyncLoginState = function (lenovoid_wust) {
        if (lenovoid_wust) {
            if (mt_user.option.cb) {
                mt_user.option.cb(lenovoid_wust);
            }
        } else {
            if (mt_user.option.cb) {
                mt_user.option.cb("");
            }
        }
    }


    //login create lenovoID cookie
    mt_user.setAccess = function (state_time, username, st, cb) {
        if (checkStr(st)) {
            console.log("check st success");
            console.log("check checkbox state_time" + state_time);
            var url = passportStAccess + "?realm=" + mt_user.option.realm + "&wust=" + st + "&username=" + username + "&state_time=" + state_time;
            var iframe1 = buildIFrame("set_lenovoid", url, function () {
                if (cb) {
                    cb(true);
                }
            });
            document.body.appendChild(iframe1);
        } else {
            console.log("check st error");
            if (cb) {
                cb(false);
            }
        }
    }

    //logout
    mt_user.logout = function (cb) {
        var url = passportLogout + mt_user.option.realm;
        var iframe1 = buildIFrame("logout_cookie", url, function () {
            mt_user.user = null;
            if (cb) {
                cb(true);
            }
        });
        document.body.appendChild(iframe1);
    }

    //check login
    mt_user.isLogin = function () {
        if (mt_user.user && mt_user.user.status) {
            return true;
        } else {
            return false;
        }
    }


    /////////////////////////////utils function//////////////////////////////////////////////

    function checkStr(str) {
        if (str != null && str != undefined && str != "") {
            return true;
        } else {
            return false
        }
    }


    function buildIFrame(id, url, callback) {
        var iframe = document.createElement('iframe');
        iframe.src = url;
        iframe.style.display = "none";
        iframe.id = id;
        iframe.setAttribute("sandbox", "allow-same-origin");
        if (iframe.attachEvent) {
            iframe.attachEvent("onload", function () {
                callback();
            });
        } else {
            iframe.onload = function (e) {
                callback();
            };
        }
        return iframe;
    }


    window.mt_user = mt_user;


    function ass(){
        let v1 = $("#lsession").val();
        let v2 = $("#lenovoId").val();
        let v3 = $("#cerpreg_passprt").val();
        mt_user.setAccess(v1, v2, v3, function () {
            alert("OK");
        });
    }

</script>
</html>